package com.itlong.cloud.utils.encrypt;

import java.util.Map;

/**
 * 统一加密类
 */
public class EncryptUtil {
    //AES加密 +MD5验签(默认)
    public static final String AES = "0";
    //国密SM4加密 + SM3验签
    public static final String SMS = "1";

    /**
     * <desc>
     * 解密接口
     * </desc>
     *
     * @param sSrc
     * @param sKey
     * @param type 加密方式
     * @return java.lang.String
     * @author maju
     * @createDate 2021/5/14
     */

    public static String decrypt(String sSrc, String sKey, String type) throws Exception {
        if (SMS.equals(type)) {
            return SMEncryptUtil.decryptStr(sSrc, sKey);
        }
        return DesEncryptUtil.aesDecrypt(sSrc, sKey);
    }

    /**
     * <desc>
     * 加密接口
     * </desc>
     *
     * @param sSrc
     * @param sKey
     * @param type 加密方式
     * @return java.lang.String
     * @author maju
     * @createDate 2021/5/14
     */

    public static String encrypt(String sSrc, String sKey, String type) throws Exception {
        if (SMS.equals(type)) {
            return SMEncryptUtil.encryptHex(sSrc, sKey);
        }
        return DesEncryptUtil.aesEncryptSDK(sSrc, sKey);
    }

    /**
     * <desc>
     * sdkV3版本验签方法
     * </desc>
     *
     * @param params
     * @param appSign
     * @param appsecret
     * @return
     */
    public static boolean checkSDKV3Sign(Map<String, Object> params, String appsecret, String appSign, String type, String... values) {
        if (SMS.equals(type)) {
            return SMEncryptUtil.checkSDKV3Sign(params, appsecret, appSign, values);
        }
        return Md5EncryptUtil.checkSDKV3Sign(params, appsecret, appSign, values);
    }

    /**
     * <desc>
     * sdkv3计算sign
     * </desc>
     *
     * @param params
     * @param appsecret
     * @return
     */
    public static String getSDKV3Sign(Map<String, Object> params, String appsecret, String type, String... values) {
        if (SMS.equals(type)) {
            return SMEncryptUtil.getSDKV3Sign(params, appsecret, values);
        }
        return Md5EncryptUtil.getSDKV3Sign(params, appsecret, values);
    }


    /**
     * <desc>
     * 返回加密内容sdkV3
     * </desc>
     *
     * @param appsecret 开发者用户密码
     * @return
     * @author Qiang.S
     * @createDate 2018/12/27
     */
    public static Map<String, Object> getEncryptResultSDKV3(Object result, String appsecret, String type) {
        if (SMS.equals(type)) {
            return SMEncryptUtil.getEncryptResultSDKV3(result, appsecret);
        }
        return DesEncryptUtil.getEncryptResultSDKV3(result, appsecret);
    }
}
